package uk.co.q3c.deplan.client.domain.task;

import java.util.List;

import uk.co.q3c.deplan.client.domain.dag.ChangeInvalidException;
import uk.co.q3c.deplan.client.domain.dag.CycleDetectedException;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;

public class Util {

	private static void replaceReferences(Task oldTask, BaseTask newTask) throws CycleDetectedException, ChangeInvalidException,
			PersistenceReferenceException {
		// remove old references in dependencies
		// do this first because new task is a summary and isn't allowed
		// dependencies
		List<Task> successors = oldTask.successors();
		List<Task> predecessors = oldTask.predecessors();
		for (Task successor : successors) {
			successor.predecessors()
					.remove(oldTask);
		}
		for (Task predecessor : predecessors) {
			predecessor.successors()
					.remove(oldTask);
		}

		// remove from assigned resource task list
		// if (oldTask instanceof BaseTask) {
		// BaseTask rt = (BaseTask) oldTask;
		// Resource resource = rt.assignedResource;
		// if (resource != null) {
		// resource.removeTask(rt);
		// }

		// }
		// transfer to parent
		newTask.setParentTask(oldTask.getParentTask());
		// this will also remove old task from parent subtasks
		oldTask.setParentTask(null);

	}
}
